<?php

namespace App\Observers;

use App\Models\Product;
use App\Models\Schedule;

class ProductObserver
{
    /**
     * 监听创建前事件.
     *
     * @param Product $model
     * @return void
     */
    public function creating(Product $model)
    {

    }

    /**
     * 监听创建事件.
     *
     * @param Product $model
     * @return void
     */
    public function created(Product $model)
    {

    }

    public function updating(Product $model)
    {

    }

    /**
     * 监听更新后事件.
     *
     * @param Product $model
     * @return void
     */
    public function updated(Product $model)
    {
        //更新时间安排
        if ($model->isDirty('is_show')) {
            if ($model->is_show == 0) {
                foreach ($model->schedules->where('status', '=', 0) as $schedule) {
                    if ($schedule->status != 2) {
                        $schedule->status = 3;
                        $schedule->save();
                    }
                }
            }
            if ($model->is_show == 1) {
                foreach ($model->schedules->where('status', '=', 3) as $schedule) {
                    $schedule->status = 0;
                    $schedule->save();
                }
            }
        }
    }

    /**
     * 监听删除事件.
     *
     * @param Product $model
     * @return void
     */
    public function deleting(Product $model)
    {

    }

    /**
     * 监听删除事件.
     *
     * @param Product $model
     * @return void
     */
    public function deleted(Product $model)
    {

    }

}
